﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Petroleum.Tool;
using System.Data.SQLite;
using Petroleum.DB.Model;
using System.Reflection;
using HZH_Controls.Forms;
using HZH_Controls.Controls;
using Petroleum.UC.Public;
using Petroleum.Model;

namespace Petroleum.UC
{
    public partial class IndexControl : UserControl
    {
        SQLiteHelper sqliteHelper = new SQLiteHelper(ResServer.DBURL);

        public IndexControl()
        {
            InitializeComponent();
            LinkLoad();

        }
        void LinkLoad()
        {
            #region ==最近项目==
            SQLiteDataReader reader = sqliteHelper.ReadFullTable("Project");

            //利用魔法将(SQLite)DataReader转换为List
            List<Project> projects = EntityTranslate.Translate<Project>(reader);
            projects = projects.OrderByDescending(m => m.LastTime).ToList();
            int proIndex = 0;
            foreach (Control item in panelPro.Controls)
            {
                if (item is Label || item is LinkLabel)
                {
                    if (proIndex == projects.Count) break;
                    item.Text = projects[proIndex].ProjectName;
                    item.Name = projects[proIndex].Id;
                    (item as LinkLabel).LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.ProjectLinkClicked);
                    proIndex++;
                }
            }
            #endregion

            #region ==最近模板==
            SQLiteDataReader templeReader = sqliteHelper.ReadFullTable("Temple");
            List<Temple> temples = EntityTranslate.Translate<Temple>(templeReader);

            temples = temples.OrderByDescending(m => m.LastTime).ToList();
            int templeIndex = 0;
            foreach (Control item in panleTemple.Controls)
            {
                if (item is Label || item is LinkLabel)
                {
                    if (templeIndex == temples.Count) break;
                    item.Text = temples[templeIndex].Name;
                    item.Name = temples[templeIndex].Id;

                    (item as LinkLabel).LinkClicked += new LinkLabelLinkClickedEventHandler(this.TempleLinkClicked);
                    templeIndex++;
                }
            }
            #endregion
        }
        private void ProjectLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LinkLabel item = (LinkLabel)sender;
            sqliteHelper.UpdateValues("Project", new string[] { "LastTime" }, new string[] { DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") }, "Id", item.Name, "=");
            FrmTips.ShowTipsInfo(new FormMain(), item.Name + "=项目=" + item.Text);

        }
        private void TempleLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LinkLabel item = (LinkLabel)sender;
            sqliteHelper.UpdateValues("Temple", new string[] { "LastTime" }, new string[] { DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") }, "Id", item.Name, "=");
            Session.TemplateFile = item.Text;
            FrmTips.ShowTipsInfo(new FormMain(), item.Name + "=模板=" + item.Text);
            
        }

        private void ucBtnExt3_BtnClick(object sender, EventArgs e)
        {
            InitializeComponent();

            LinkLoad();
        }

        private void ucBtnExt1_BtnClick(object sender, EventArgs e)
        {
            new FormChoice().ShowDialog(this);
            if (Session.WellName == string.Empty || Session.TemplateFile == string.Empty)
            {
                LogHelper.WriteLog("用户取消输入，不执行创建项目操作");
                return;
            }
            Temple temple = new Temple()
            {
                Id = Guid.NewGuid().ToString(),
                Name = Session.TemplateFile
            };
            Project project = new Project()
            {
                Id = Guid.NewGuid().ToString(),
                ProjectName = Session.WellName,
                TempleId = temple.Id
            };
            //向数据库插入
            sqliteHelper.InsertValues("Temple", new string[] { temple.Id, temple.Name, temple.CreateTime, temple.LastTime, temple.State.ToString() });
            sqliteHelper.InsertValues("Project", new string[] { project.Id, project.TempleId, project.ProjectName, project.CreateTime, project.LastTime, project.State.ToString() });
            //保存到Session
            Session.ProjectId = project.Id;
            //重新刷新历史项目
            LinkLoad();
            LogHelper.WriteLog(string.Format("用户创建项目成功,项目ID:{0},模板ID:{1}", project.Id, temple.Id));

        }
    }
}
